package com.paiai.lslease.service.apphouse;

import com.paiai.lslease.auto.dao.AppHouseDAO;
import com.paiai.lslease.auto.dao.AppLockDAO;
import com.paiai.lslease.auto.model.*;
import com.paiai.lslease.dao.apphouse.AppLockTenantInfoDAO;
import com.paiai.lslease.dao.apphouse.AppTenantInfoDAO;
import com.paiai.lslease.model.AppHouseInfo;
import com.paiai.lslease.model.AppLandlordAuthHouseInfo;
import com.paiai.lslease.model.AppLockInfo;
import com.paiai.lslease.model.AppLockTenantInfo;
import com.paiai.lslease.service.appuser.AppSysUserService;
import com.paiai.lslease.service.landlordauth.AppLandlordAuthService;
import com.paiai.lslease.util.FatherToChildUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AppLockServiceImpl implements AppLockService {

    private static final Logger logger = LoggerFactory.getLogger(AppLockServiceImpl.class);

    private AppLockDAO appLockDAO;
    private AppSysUserService appSysUserService;
    private AppTenantInfoDAO appTenantInfoDAO;
    private AppLockTenantService appLockTenantService;
    private AppSocketService appSocketService;
    private AppHouseService appHouseService;
    private AppHouseDAO appHouseDAO;
    private AppLockTenantInfoDAO appLockTenantInfoDAO;
    @Autowired
    private AppLandlordAuthService appLandlordAuthService;

    public void setAppLockDAO(AppLockDAO appLockDAO) {
        this.appLockDAO = appLockDAO;
    }

    public void setAppSysUserService(AppSysUserService appSysUserService) {
        this.appSysUserService = appSysUserService;
    }

    public void setAppTenantInfoDAO(AppTenantInfoDAO appTenantInfoDAO) {
        this.appTenantInfoDAO = appTenantInfoDAO;
    }

    public void setAppLockTenantService(AppLockTenantService appLockTenantService) {
        this.appLockTenantService = appLockTenantService;
    }

    public void setAppSocketService(AppSocketService appSocketService) {
        this.appSocketService = appSocketService;
    }

    public void setAppHouseService(AppHouseService appHouseService) {
        this.appHouseService = appHouseService;
    }

    public void setAppHouseDAO(AppHouseDAO appHouseDAO) {
        this.appHouseDAO = appHouseDAO;
    }

    public void setAppLockTenantInfoDAO(AppLockTenantInfoDAO appLockTenantInfoDAO) {
        this.appLockTenantInfoDAO = appLockTenantInfoDAO;
    }

    public AppLock insertLock(AppLock appLock) throws Exception {
        appLockDAO.insert(appLock);
        return selectLockById(appTenantInfoDAO.selectNeweastAppLockId());
    }

    public boolean deleteLockById(int lockId) throws Exception{
        //删除锁下的所有租客
        if (!appLockTenantService.deleteAllLockTenantByLockId(Integer.parseInt(lockId + ""))) {
            logger.info("删除设备下的所有租客失败");
            return false;
        }
        //删除锁
        AppLock appLock = new AppLock();
        appLock.setId(lockId);
        appLock.setItem1("-1");
        appLock.setUpdateTime(new Date());
        return appLockDAO.updateByPrimaryKeySelective(appLock) > 0;
    }

    public AppLock modifyLock(AppLock appLock) throws Exception {
        if (appLock.getSocketId() != null && appLock.getSocketId() > 0) {//设置了更新设备的插座ID
            //先检测要更新插座的信息是否存在
            AppSocket appSocket = appSocketService.selectSocket(appLock.getSocketId());
            if (appSocket == null) {
                logger.info("要更新插座的信息不存在");
                return null;
            }
        }
        //检测当前设备是否存在插座信息
        AppLock appLock1 = selectLockById(appLock.getId());
        if (appLock1.getSocketId() != null && appLock1.getSocketId() > 10000) {//存在，标记为删除
            appSocketService.deleteSocket(appLock1.getSocketId());
        }
        //更新插座信息
        appLock.setUpdateTime(new Date());
        if (appLockDAO.updateByPrimaryKeySelective(appLock) <= 0) {
            throw new Exception();
        }
        return selectLockById(appLock.getId());
    }

    public List<AppLockInfo> selectHouseAllLock(int houseId) throws Exception {
        //查询单个房源的所有设备列表
        List<AppLockInfo> appLockInfoList = new ArrayList<AppLockInfo>();
        AppLockExample appLockExample = new AppLockExample();
        appLockExample.createCriteria().andHouseIdEqualTo(houseId).andItem1EqualTo("1");
        List<AppLock> appLockList = appLockDAO.selectByExample(appLockExample);
        for (AppLock appLock : appLockList) {
            AppLockInfo appLockInfo = new AppLockInfo();
            FatherToChildUtils.fatherToChild(appLock, appLockInfo);
            //查询单个设备的所有租客列表
//                AppLockTenantExample appLockTenantExample = new AppLockTenantExample();
//                appLockTenantExample.createCriteria().andLockIdEqualTo(appLock.getId()).andItem1EqualTo("1");
//                List<AppLockTenant> appLockTenantList = appLockTenantDAO.selectByExample(appLockTenantExample);
            List<AppLockTenantInfo> appLockTenantInfoList = appLockTenantInfoDAO.selectAllLockTenantInfoByLockId(appLock.getId() + "");
//                appLockInfo.setAppLockTenantList(appLockTenantList);
            appLockInfo.setAppLockTenantInfoList(appLockTenantInfoList);
            appLockInfoList.add(appLockInfo);
        }
        return appLockInfoList;
    }

    public List<AppLock> selectUserAllDevice(int userId) throws Exception {
        List<AppLock> appLockList = new ArrayList<AppLock>();
        //查询用户的所有房源列表
        List<AppHouseInfo> appHouseInfoList = appHouseService.getApphouseInfoList(userId);
        if (appHouseInfoList == null) {
            return null;
        }
        for (AppHouseInfo appHouseInfo : appHouseInfoList) {
//            appLockList.addAll(appHouseInfo.getAppLockInfoList());
        }
        return appLockList;
    }

    public AppLock selectLockById(int lockId) throws Exception {
        AppLock appLock = appLockDAO.selectByPrimaryKey(lockId);
        return appLock != null && "1".equalsIgnoreCase(appLock.getItem1()) ? appLock : null;
    }

    public boolean lockExistById(int lockId) throws Exception {
        return selectLockById(lockId) != null;
    }

    public List<AppLock> selectOnlyLandlordAllLock(int landlordId) throws Exception {
        List<AppLock> appLockList = new ArrayList<AppLock>();
        AppHouseExample appHouseExample = new AppHouseExample();
        appHouseExample.createCriteria().andLandlordIdEqualTo(landlordId).andItem1EqualTo("1");
        List<AppHouse> appHouseList = appHouseDAO.selectByExample(appHouseExample);
        for (AppHouse appHouse : appHouseList) {
            appLockList.addAll(selectHouseAllLock(appHouse.getId()));
        }
        return appLockList;
    }

    public List<AppLockTenant> selectLandlordAllLockTenant(int landlordId) throws Exception {
        List<AppLockTenant> appLockTenantList = new ArrayList<AppLockTenant>();
        List<AppLock> appLockList = selectOnlyLandlordAllLock(landlordId);
        for (AppLock appLock : appLockList) {
            appLockTenantList.addAll(appLockTenantService.selectAllLockTenant(appLock.getId()));
        }
        return appLockTenantList;
    }

    public boolean lockBelongLandlord(int lockId, int landlordId) throws Exception {
        List<AppLock> appLockList = selectOnlyLandlordAllLock(landlordId);
        boolean belong = false;
        for (AppLock appLock : appLockList) {
            if (appLock.getId() == lockId) {
                belong = true;
                break;
            }
        }
        return belong;
    }

    @Override
    public boolean lockBelongUser(int lockId, int userId) throws Exception {
        if (appSysUserService.userBelongLandlord(userId) && lockBelongLandlord(lockId, userId)) {//设备是否属于房东
            return true;
        }
        if (appSysUserService.userBelongLandlordAuthManagerById(userId)) {//设备是否属于被授权的管理员
            List<AppLandlordAuthHouseInfo> appLandlordAuthHouseInfoList = appLandlordAuthService.selectAppLandlordAuthListByUserId(userId);
            if (appLandlordAuthHouseInfoList != null) {
                for (AppLandlordAuthHouseInfo appLandlordAuthHouseInfo : appLandlordAuthHouseInfoList) {
                    if (lockBelongHouse(lockId, appLandlordAuthHouseInfo.getId())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public boolean lockTenantBelongLandlord(int lockTenantId, int landlord) throws Exception {
        List<AppLockTenant> appLockTenantList = selectLandlordAllLockTenant(landlord);
        boolean belong = false;
        for (AppLockTenant appLockTenant : appLockTenantList) {
            if (lockTenantId == appLockTenant.getTenantId()) {
                belong = true;
                break;
            }
        }
        return belong;
    }

    @Override
    public boolean lockBelongHouse(int lockId, int houseId) throws Exception {
        //查询单个房源的所有设备列表
        AppLockExample appLockExample = new AppLockExample();
        appLockExample.createCriteria().andHouseIdEqualTo(houseId).andItem1EqualTo("1");
        List<AppLock> appLockList = appLockDAO.selectByExample(appLockExample);
        if (appLockList != null) {
            for (AppLock appLock : appLockList) {
                if (lockId == appLock.getId()) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean lockExistByHouseIdAndLockMac(int houseId, String lockMac) throws Exception {
        AppLockExample appLockExample = new AppLockExample();
        appLockExample.createCriteria().andHouseIdEqualTo(houseId).andMacAddressEqualTo(lockMac).andItem1EqualTo("1");
        List<AppLock> appLockList = appLockDAO.selectByExample(appLockExample);
        return appLockList != null && appLockList.size() > 0;
    }
}
